package com.aiyun.dubbo.web;


import java.util.List;

import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
@RefreshScope
public class EnvResource {

    @Value("${configserver:}")
    String configStatus;

    private final Environment environment;

    private final DiscoveryClient discoveryClient;

    public EnvResource(Environment environment, DiscoveryClient discoveryClient) {
        this.environment = environment;
		this.discoveryClient = discoveryClient;
	}

	@GetMapping("/config")
	public ResponseEntity<String> getCustomerConfig(String key) {
		String property = environment.getProperty(key);
		return ResponseEntity.ok(property);
	}

    @GetMapping("/config/env")
    public ResponseEntity<String> getEnvConfig() {
        String property = environment.getProperty("configserver");
        if (StringUtils.isEmpty(property)) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(property);
    }

    @GetMapping("/config/value")
    public ResponseEntity<String> getValConfig() {
        return ResponseEntity.ok(configStatus);
    }

    @GetMapping("/services")
    public ResponseEntity<List<String>> getServices() {
		List<String> services = discoveryClient.getServices();
		return ResponseEntity.ok(services);
	}
}
